Object dematerialization is a technique for optimizing object oriented programs, based on the fact that objects are often created and destroyed within the same scope. It is common that for such temporary objects, not all fields are used. When a compiler detects that an object only lives within a single scope and that only a few fields are really put to use, then the compiler can (instead of allocating a complete object) put only the used fields into registers or on the stack. Every object method must be inlined to work with the new locations for the fields. The result is that no real object allocation in memory has to be performed. If very few fields are used from the original object, then the performance speedups come from not having to initialize all of the object memory, nor having to run the full constructor. The field accesses will also be faster if the fields are moved into registers. For garbage collecting languages, not having to allocate the object is also a significant saving, since avoided allocations means less frequent garbage collects.
The following example shows how object dematerialization is applied to a calculation of a person's age, which is a common problem in industrial software code. Instead of simply subtracting the birth year from the current year, a Person object can be created that encapsulates the name and the birth year of the person. The name is empty since the code only uses the object method get_age to calculate the age. A get_age function can simply subtract the birth year from the current year:
int calculate age(int birth, int current year){  Person p = new Person(””, birth);  return p.get_age(current_year);}After inlining has been performed the equivalent pseudo-code will be:
int calculate age(int birth, int current year){  Person p = new Person( );  p.name = ””;  p.birth_year = birth;  return current_year-p.birth_year;}The compiler can detect that p does not escape the scope, and that only certain fields are used. Thus it can perform both object dematerialization (i.e. move the name and birth_year into local variables), and the removal of unused variables (i.e. the name variable):
int calculate age(int birth, int current year){  int birth_year = birth;  return current_year-birth_year;}In the above example it is easy to determine if the object escapes the scope or not. Unfortunately current Java programs do not offer many opportunities for plain object dematerialization. For languages with explicit destructors, such as C++, it is often obvious to the compiler if an object escapes or not. However, this is not possible in garbage collected languages without explicit destructors, such as Java. Also it is common that an object is injected into the code path as an argument, or as the return value from a function. In these cases the object must be considered to escape, since the system has no control over the code outside of the current scope.